package com.moli.iov.admin.service.impl;

import com.moli.iov.admin.config.FtpConfig;
import com.moli.iov.admin.model.vo.req.TboxUpgradeFileReqVo;
import com.moli.iov.admin.service.FtpService;
import com.moli.iov.admin.utils.FtpClientUtils;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.SnowflakeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Date;

@Service
@Slf4j
public class FtpServiceImpl implements FtpService {

    @Resource
    FtpConfig ftpConfig;

    @Override
    public RestResponse upload(MultipartFile multipartFile, TboxUpgradeFileReqVo req) {
        
        long startTime = System.currentTimeMillis();
        String fileName = SnowflakeUtils.getId()+"_"+multipartFile.getOriginalFilename();

        String ip = ftpConfig.getIp();
        Integer port = ftpConfig.getPort();
        String userName = ftpConfig.getUsername();
        String pass  = ftpConfig.getPassword();
        String path = ftpConfig.getPath();

        log.info("升级包上传>>>ip:{},port:{},userName:{},password:{},path:{}",ip,port,userName,pass,path);

//        String ip = "172.16.100.74";
//        Integer port = 21;
//        String userName = "pro";
//        String pass  = "moliprod";
//        String path = "/history/iov/upgrade";


        try {
            FtpClientUtils.uploadFile(ip,port,userName,pass,path,fileName,multipartFile.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
            return RestResponse.failure("上传文件失败");
        }
//        ftp://dev:5GQvFT455gc9@103.235.230.101:21/SHENGJI(V1.05).fx1
        return RestResponse.success(fileName);
    }

    private String getPath(String ip, Integer port, String username, String password, String path, String fileName) {
        String res = "ftp://"+username+":"+password+"@"+ip+":"+port+path+"/"+fileName;
        return res;
    }
}
